package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.InvoiceManagerBean;
import com.longrise.common.utils.AppUtil;

import java.util.List;

import static java.util.Locale.CHINESE;

/**
 * Created by Administrator on 2017/10/22.
 *发票管理adapter
 */

class InvoiceManagerRcvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    public static final String TAG = "InvoiceManagerRcvAdapter";
    private List<InvoiceManagerBean> data;
    private boolean isChecked;
    //接口实例
    public OnItemClickListener onItemClickListener;

    private Context mContext;


    public void refresh(List<InvoiceManagerBean> list,Context mContext) {
        this.data = list;
        this.mContext = mContext;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_invoice_manager_rcv, null);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

            final InvoiceManagerBean bean = data.get(position);
//            ((ItemViewHolder) holder).itemView.setTag(position);
        ((ItemViewHolder) holder).tv_item_header.setText(bean.getInvoiceHeader());
        ((ItemViewHolder) holder).tv_item_invoice_time.setText(bean.getPayTime());
        String payMoney = String.format(CHINESE, "%.2f", bean.getPayMoney());
        ((ItemViewHolder) holder).tv_item_invoice_money.setText("¥" + payMoney +"");
        ((ItemViewHolder) holder).tv_invoice_num.setText("订单编号:"+bean.getOrderId());

        ((ItemViewHolder) holder).tv_item_header.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = ((ItemViewHolder) holder).tv_item_header.getLineCount();
                ViewGroup.LayoutParams layoutParams = ((ItemViewHolder) holder).ll_invoice_title.getLayoutParams();
                if (lineCount >= 2) {
                    layoutParams.height = AppUtil.dip2px(62);
//                    ((ItemViewHolder) holder).ll_item_invoice.setBackgroundResource(R.drawable.bg_invoice_head_2);
                } else {
                    layoutParams.height = AppUtil.dip2px(39);
//                    ((ItemViewHolder) holder).ll_item_invoice.setBackgroundResource(R.drawable.bg_invoice_head_1);
                }
                ((ItemViewHolder) holder).ll_invoice_title.setLayoutParams(layoutParams);
            }
        });

            //设置Tag
        ((ItemViewHolder)holder).view.setTag(position);
        ((ItemViewHolder)holder).view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, (Integer) v.getTag());
                isChecked = bean.isSelected();
                if (isChecked){
                    ((ItemViewHolder) holder).check.setChecked(true);
                }else {
                    ((ItemViewHolder) holder).check.setChecked(false);
                }

            }
        });

       // 用户点击checkbox行为会需要增加监听来改变checkStates对应项的状态
        //这里为一点小坑，就是上面所说的滑屏选中状态丢失的现象，所以在checkbox的选中监听里我们需要将checkbox
        //的选择状态和选择条目的position记录下来，在bindviewholder中设置选中，可以完美解决
        ((ItemViewHolder) holder).check.setChecked(bean.isSelected());
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {
        CheckBox check;
        TextView tv_item_header;
        TextView tv_item_invoice_time;
        TextView tv_item_invoice_money;
        private View view;
        RelativeLayout ll_item_invoice;
        LinearLayout ll_invoice_title;
        LinearLayout ll_invoice_manager;
        TextView tv_invoice_num;

        public ItemViewHolder(View view) {
            super(view);
            this.view = view;
            check = (CheckBox) view.findViewById(R.id.rb_item_invoice);
            tv_item_header = (TextView) view.findViewById(R.id.tv_item_header);
            tv_item_invoice_time = (TextView) view.findViewById(R.id.tv_item_invoice_time);
            tv_item_invoice_money = (TextView) view.findViewById(R.id.tv_item_invoice_money);
            ll_item_invoice= (RelativeLayout) view.findViewById(R.id.ll_item_invoice);
            ll_invoice_title= (LinearLayout) view.findViewById(R.id.ll_invoice_title);
            ll_invoice_manager= (LinearLayout) view.findViewById(R.id.ll_invoice_manager);
            tv_invoice_num = (TextView) view.findViewById(R.id.tv_invoice_manager_num);
        }
    }

    //设置点击事件
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


//    //接口回调设置点击事件
//    public interface OnItemClickListener {
//        //点击事件
//        void onItemClickListener(View view, int position);
//    }
}
